package cn.com.pansky.action.dict;

import java.util.Set;

import net.vsame.url2sql.action.CommonAction;
import cn.com.pansky.sdk.API;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class DictionaryAction extends CommonAction{
	
	/**
	 * 经办机构查询
	 * [{"AAA146":"宝鸡市","AAB301":"610300"},{"AAA146":"市辖区","AAB301":"610301"}]
	 */
	public void  organization() {
		JSONObject param = new JSONObject();
		param.put("AAC002", "1"); // 身份证号
		param.put("AAA121", "QAA26"); //标识
		param.put("AAB301", getParam("appId").substring(0, 4)); // 行政区划
		JSONObject object = API.sendController("organizationController.json", param);
		if(object.getBooleanValue("SUCCESS")) {
			handleSuccess(object);
		}
	}
	
	/**
	 * 乡镇查询
	 * [{"AAB301":"61030301","AAA146":"西关办事处测试"},{"AAB301":"61030302","AAA146":"中山西路街道办事处"}]
	 */
	public void streetTown() {
		JSONObject param = new JSONObject();
		param.put("AAC002", "1"); // 身份证号
		param.put("AAA121", "QF10000"); //标识
		param.put("AAB301", getParam("AAB301")); // 行政区划
		JSONObject object = API.sendController("streetTownController.json", param);
		if(object.getBooleanValue("SUCCESS")) {
			JSONObject msg = object.getJSONObject("MSG");
			JSONArray array = msg.getJSONArray("AAF015");
			if (array.size()>0) {
				String json = "[";
				for(int i = 0; i < array.size(); i++) {
					JSONObject obj = JSONObject.parseObject(array.get(i).toString());
					Set<String> set = obj.keySet();
					json+="{\"AAB301\":\""+set.iterator().next()+"\",\"AAA146\":\""+obj.getString(set.iterator().next())+"\"},";
				}
				json = json.substring(0, json.length()-1);
				json+="]";
				context.put("result", JSON.parse(json));
			}else{
				context.getDatas().put("result", new JSONArray());
			}
		}
	}
	
	/**
	 * 村查询
	 * [{"AAB301":"14000817","AAA146":"金台测试村"},{"AAB301":"11279333","AAA146":"南门口社区"}]
	 */
	public void village() {
		JSONObject param = new JSONObject();
		param.put("AAC002", "1"); // 身份证号
		param.put("AAA121", "QF10001"); //标识
		param.put("AAF015", getParam("AAB301")); //镇
		JSONObject object = API.sendController("villageController.json", param);
		JSONArray array = new JSONArray();
		if(object.getBooleanValue("SUCCESS")) {
			JSONObject msg = object.getJSONObject("MSG");
			array = msg.getJSONArray("AAZ070");
			if (array.size()>0) {
				String json = "[";
				for(int i = 0; i < array.size(); i++) {
					JSONObject obj = JSONObject.parseObject(array.get(i).toString());
					Set<String> set = obj.keySet();
					json+="{\"AAB301\":\""+set.iterator().next()+"\",\"AAA146\":\""+obj.getString(set.iterator().next())+"\"},";
				}
				json = json.substring(0, json.length()-1);
				json+="]";
				context.put("result", JSON.parse(json));
			}else{
				context.put("result",new JSONArray());
			}
		}
	}
	
	/**
	 * 缴费档次
	 */
	public void paygrade() {
		JSONObject param = new JSONObject();
		param.put("AAC002", "1"); // 身份证号
		param.put("AAA121", "QAA05"); //标识
		param.put("AAA118", getParam("AAA118")); // 户口性质
		param.put("AAA027", getParam("AAA027")); // 行政区划
		JSONObject object = API.sendController("paygradeController.json", param);
		JSONArray o = object.getJSONArray("MSG");
		context.put("result", o);
	}
	
	/**
	 * 成功后处理返回值
	 * @param obj
	 */
	private void handleSuccess(JSONObject obj) {
		JSONArray array = new JSONArray();
		JSONArray c = new JSONArray(); ;
		if(obj.getString("SUCCESS").equals("true")) {
			array = obj.getJSONArray("MSG");
			for (int i = 0; i < array.size(); i++) {
				JSONObject temp = (JSONObject) array.get(i);
				String aab301 = temp.getString("AAB301");
				if("610300".equals(aab301) || "610301".equals(aab301)) {
					c.add(temp);
				}
			}
			array.removeAll(c);
		}
		context.put("result", array);
	}
	
	public static void main(String[] args) {
		JSONObject param = new JSONObject();
		param.put("AAC002", "14262219920107153x"); // 身份证号
		param.put("AAA121", "QAA26"); //标识
		param.put("AAB301", "6103"); // 行政区划
		System.out.println(API.sendController("organizationController.json", param));
	}
	
}
